using CfgTable;
using IQIGame.Onigao.Framework;
using NetProtocol.Enum;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public abstract partial class LevelPlayBaseEntity : TViewReusableEntity, IQuadTreeObject, IBVHObject2D
    {
        public override int id => GetDynEntityData().id;
        public override int mcid => GetDynEntityData().mcid;
        public override int mapSymbolCid => GetDynEntityData().mapSymbolCid;
        public override IReadOnlyList<int> offeredQuests => GetDynEntityData().offeredQuests;
        public abstract bool active { get; }

        public abstract float x { get; }

        public abstract float y { get; }

        public abstract float z { get; }

        public abstract int dirEuler { get; }

        public virtual byte interactType { get; }

        /// <summary>
        /// 实体资源配置表，默认为空
        /// </summary>
        public override CfgPhysicalEntity cfgEntity { get; }

        /// <summary>
        /// bvh tree，aabb碰撞检测用
        /// </summary>
        public virtual BVHBounds2D bvhBounds { get => default; set { } }

        /// <summary>
        ///  quadtree， 附近实体查询用
        /// </summary>
        public Vector2 quadTreePos { get => _qtPos; set => _qtPos = value; }
        private Vector2 _qtPos;
        public override Vector3 unityPosOfData => GameMathUtil.LogicPos2UnityPos(x, y, z);
        public override Quaternion unityRotationOfData => GameMathUtil.IntEuler2Quaternion(GetDynEntityData().dirEuler);
        public override TNavigationController navigationCtrl => LevelPlayModule.Instance.levelPlay.regionCtrl.navigationCtrl;

        #region 生命周期
        /// <summary>
        /// 实体创建时
        /// </summary>
        protected virtual void OnEntityCreate()
        {
            EntityAddComponents();
        }

        /// <summary>
        /// 实体出生时
        /// </summary>
        public abstract void OnEntityBirth();

        /// <summary>
        /// 实体初始添加组件
        /// </summary>
        public abstract void EntityAddComponents();

        public virtual void BroadcastComponentEvent(TComponentEvent eventType)
        {
            BroadcastComponentEvent(eventType, 0);
        }

        public virtual void BroadcastComponentEvent<T>(TComponentEvent eventType, T arg)
        {
            foreach (var kv in componentMap)
            {
                if (kv.Value is TViewComponent component)
                {
                    component.OnComponentEvent(eventType, arg);
                }
            }
        }

        #endregion

        #region 实体方法
        public virtual DynLevelEntity GetDynLevelEntity()
        {
            return null;
        }

        public virtual BaseDynLevelEntity GetDynEntityData()
        {
            return GetDynLevelEntity();
        }

        public virtual LevelBuffHolder GetBuffHolder()
        {
            return null;
        }

        public virtual void SyncEntityPos(float x, float y, float z, float speedRatio, EntityMoveDirType dirType, LevelMoveReason reason)
        {

        }

        public virtual void SetEntityActive(bool active)
        {

        }

        public virtual bool ContainTag(ushort tagId)
        {
            return false;
        }

        /// <summary>
        /// 刷新任务显示
        /// </summary>
        public virtual void RefreshQuestState()
        {

        }
        #endregion


        #region 工具方法
        public virtual Vector3 GetUnityVec3Pos()
        {
            return GameMathUtil.LogicPos2UnityPos(x, y, z);
        }

        public virtual Vector3 GetRecastVec3Pos()
        {
            return GetUnityVec3Pos();
        }

        public virtual Vector2 GetUnityVec2Pos()
        {
            return GameMathUtil.LogicPos2UnityPosV2(x, z);
        }

        #endregion


        #region 属性

        public virtual int GetDefaultSpeed()
        {
            return 0;
        }

        #endregion

        public override void OnReset()
        {
            _qtPos = default;
        }
    }
}
